package 剑指offer.Month7_07.day14;

import java.util.ArrayList;

public class 顺时针打印矩阵 {
    public ArrayList<Integer> printMatrix(int [][] matrix) {
        int left = 0,right = matrix[0].length - 1;
        int up = 0,down = matrix.length - 1;
        ArrayList<Integer> list = new ArrayList<>();
        while(left <= right && up <= down){
            for (int i = left;i <= right;i++){
                list.add(matrix[up][i]);
            }
            up++;
            for (int i = up;i <= down;i++){
                list.add(matrix[i][right]);
            }
            right--;
            if(up <= down){
                for (int i = right;i >= left;i--){
                    list.add(matrix[down][i]);
                }
                down--;
            }
            if(left <= right){
                for (int i = down;i >= up;i--){
                    list.add(matrix[i][left]);
                }
                left++;
            }
        }
        return list;
    }
}
